package db;

import base.ArrayOfFood;
import base.Food;
import java.io.*;
import java.util.Vector;
import javax.microedition.rms.*;

/**
 *
 * @author Shayan
 */
public class FoodManager {

    private final static String FOOD_TABLE = "FOODS";

    public static void deleteAll() throws RecordStoreException {
        RecordStore.deleteRecordStore(FOOD_TABLE);
    }

    public static void persist(Food food)
            throws RecordStoreException, IOException {
        RecordStore rs = RecordStore.openRecordStore(FOOD_TABLE, true);

        persist(rs, food);

        rs.closeRecordStore();
    }

    public static void persist(ArrayOfFood foods)
            throws RecordStoreException, IOException {
        RecordStore rs = RecordStore.openRecordStore(FOOD_TABLE, true);

        for (int i = 0; i < foods.getFoods().length; i++) {
            persist(rs, foods.getFoods()[i]);
        }

        rs.closeRecordStore();
    }

    private static void persist(RecordStore rs, Food food)
            throws IOException, RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException {
        byte[] data = food.getData();

        int recordId = DatabaseHelper.findRecordId(rs, food.getId());
        if (recordId == -1) {
            rs.addRecord(data, 0, data.length);
        } else {
            rs.setRecord(recordId, data, 0, data.length);
        }
    }

    public static Food getById(String id)
            throws RecordStoreException, RecordStoreNotOpenException, InvalidRecordIDException, IOException {
        RecordStore rs = RecordStore.openRecordStore(FOOD_TABLE, true);

        Food result = null;
        byte[] data = DatabaseHelper.findRecordById(rs, id);
        if (data != null) {
            result = Food.create(data);
        }

        rs.closeRecordStore();
        return result;
    }

    public static Food getByName(String name)
            throws RecordStoreException, RecordStoreNotOpenException, InvalidRecordIDException, IOException {
        RecordStore rs = RecordStore.openRecordStore(FOOD_TABLE, true);

        Food result = null;
        byte[] data = DatabaseHelper.findRecordByName(rs, name);
        if (data != null) {
            result = Food.create(data);
        }

        rs.closeRecordStore();
        return result;
    }

    public static Vector getAllByCategory(String categoryId)
            throws InvalidRecordIDException, RecordStoreNotOpenException, IOException, RecordStoreException {
        RecordStore rs = RecordStore.openRecordStore(FOOD_TABLE, true);

        Vector result = new Vector();

        RecordEnumeration re = rs.enumerateRecords(null, null, false);
        while (re.hasNextElement()) {
            byte[] data = re.nextRecord();

            ByteArrayInputStream bin = new ByteArrayInputStream(data);
            DataInputStream in = new DataInputStream(bin);

            in.readUTF(); // id, ignore it.
            in.readUTF(); // name, ignore it.
            if (categoryId.equalsIgnoreCase(in.readUTF())) {
                result.addElement(Food.create(data));
            }
        }

        rs.closeRecordStore();
        return result;
    }

    public static Vector getAll()
            throws InvalidRecordIDException, RecordStoreNotOpenException, IOException, RecordStoreException {
        RecordStore rs = RecordStore.openRecordStore(FOOD_TABLE, true);

        Vector result = new Vector();
        FoodComparator comparator = new FoodComparator();
        RecordEnumeration re = rs.enumerateRecords(null, comparator, false);

        while (re.hasNextElement()) {
            byte[] data = re.nextRecord();
            result.addElement(Food.create(data));
        }

        rs.closeRecordStore();
        return result;
    }
}

